package com.hmxx.myblogproject.controller;

import com.hmxx.myblogproject.mapper.MyBlogTitlesMapper;
import com.hmxx.myblogproject.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @description:
 * @Author: {27106}
 * @Date: {2021/11/19 13:00}
 */
@RestController
@RequestMapping("/picture")
public class MyBlogPictureController {
    @Value("${uploadPathImg}")
    private String uploadPathImg;
    String url="";
    @Resource
    private MyBlogTitlesMapper myBlogTitlesMapper;

    @PostMapping("/uploadPicture")
    public Object upload(@RequestParam(value = "file") MultipartFile file,@RequestParam("title_id") String titleId,HttpServletRequest request) throws IOException {
        try {
            if (file != null) {
                System.out.println("uploadPathImg = " + uploadPathImg);
                String directory = System.currentTimeMillis()+"/";
                System.out.println("directory = " + directory);
                File ImageUploadSavePath = new File(uploadPathImg + directory);
                if (!ImageUploadSavePath.exists()) {
                    boolean i=ImageUploadSavePath.mkdirs();
                    System.out.println("i = " + i);
                }
                System.out.println("图片上传的保存位置：" + ImageUploadSavePath);

                //3.给文件重新设置一个名字
                //后缀
                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
                File newFile=new File(uploadPathImg+directory+newFileName);
                System.out.println("newFile = " + newFile);

                file.transferTo(newFile);
                //协议 :// ip地址 ：端口号 / 文件目录(/images/2020/03/15/xxx.jpg)
                url = request.getScheme() + "://" +
                        request.getServerName() + ":" +
                        request.getServerPort() + "/images/" +
                        directory + newFileName;
                System.out.println("url = " + url);
                int res=myBlogTitlesMapper.updateTitleImg(titleId,url);
                if(res>0){
                    return ResponseUtil.ok();
                }else {
                    return ResponseUtil.fail(3,"更新图片失败");
                }

            }else {
                return ResponseUtil.fail(1,"图片上传失败");
            }
        } catch (Exception e) {
            return ResponseUtil.fail(2,"IO异常");
        }
    }


}
